/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2024-03-24
 * Time: 9:08
 */
class Solution {
    public int jewelleryValue(int[][] frame) {
        //1.创建dp
        //dp[i][j]表示到达该位置[i,j]的珠宝的最大价值
        int m = frame.length, n = frame[0].length;
        int[][] dp = new int[m][n];
        //2.初始化dp
        dp[0][0] = frame[0][0];
        for(int i = 1; i < m; i++) {
            dp[i][0] += dp[i-1][0]+frame[i][0];
        }
        for(int j = 1; j < n; j++) {
            dp[0][j] += dp[0][j-1]+frame[0][j];
        }
        //3.状态转移
        //4.填表顺序
        for(int i = 1; i < m; i++) {
            for(int j = 1; j < n; j++) {
                dp[i][j] = Math.max(dp[i-1][j],dp[i][j-1])+frame[i][j];
            }
        }
        //5.返回结果
        return dp[m-1][n-1];

    }

    //优化后
    public int jewelleryValue1(int[][] frame) {
        int m = frame.length, n = frame[0].length;
        int[][] dp = new int[m+1][n+1];
        //3.状态转移
        //4.填表顺序
        for(int i = 1; i <= m; i++) {
            for(int j = 1; j <= n; j++) {
                dp[i][j] = Math.max(dp[i-1][j],dp[i][j-1])+frame[i-1][j-1];
            }
        }
        //5.返回结果
        return dp[m][n];
    }


}
